package csu.rjxyg.action.userAction;

import java.util.List;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.bean.DBObject;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.util.LogUtil;

public class ChangePageAction extends BaseAction {
	private String pageName;
	private LogUtil log;
	
	public ChangePageAction(){
		log = UtilFactory.getLogUtil(getClass());
	}
	
	public String changePage(){
		String name = null;
		try{
			pageName = new String(pageName.getBytes("iso-8859-1"),"UTF-8");
			BaseUserService userSerivce = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			
			if( object == null ){
				log.info(name + "切换权限失败" );
				return this.FAIL;
			}
			//检查当前登陆用户是否有此权限
			//将权限转化为系统识别
			pageName = (String) this.getPermissionNameMap().get(pageName);
			List list = (List) object.get(UserConstant.USER_QX);
			if(!list.contains(pageName)){
				log.info(name + "切换权限失败" );
				return this.FAIL;
			}
			
			object.set(UserConstant.USER_LOGIN_QX, pageName);
			name = (String) object.get(UserConstant.USER_ID);
			userSerivce.updateUserBean(name, object);
			log.info(name + "切换权限成功" );
			String temp = (String) getPermissionMap().get(pageName);
			return temp;
			
				
		}catch(Exception e){
			e.printStackTrace();
			log.info(e.getMessage());
		}
		log.info(name + "切换权限失败" );
		return this.FAIL;
	}

	public String getPageName() {
		return pageName;
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}
	
}
